package com.ztgf.logistics.api.client.deppon;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.vo.CommodityExportNewVO;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.*;
import com.ztgf.logistics.models.bo.deppon.LogisticsBillOrderDepponSnapshotBO;
import com.ztgf.logistics.models.bo.deppon.LogisticsBillOrderDepponSnapshotSaveBO;
import com.ztgf.logistics.models.dto.deppon.LogisticsBillOrderDepponSnapshotDTO;
import com.ztgf.logistics.models.service.deppon.ILogisticsBillOrderDepponSnapshotService;
import com.ztgf.logistics.models.vo.ExportLogisticsOrderListVO;
import com.ztgf.logistics.models.vo.LogisticsBillOrderDepponSnapshotExpotVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 物流德邦日志快照表控制器
 *
 * @author mxli
 * @Date 2021-09-13 22:14:38
 */

@RestController
@RequestMapping("/api/logisticsBillOrderDepponSnapshotApi")
@Slf4j
@Api(value = "LogisticsBillOrderDepponSnapshotController",tags = "物流德邦日志快照表接口")
public class LogisticsBillOrderDepponSnapshotApiController {

    @DubboReference
    private ILogisticsBillOrderDepponSnapshotService logisticsBillOrderDepponSnapshotService;

    @ApiOperation(value = "物流德邦日志快照表列表",notes = "物流德邦日志快照表列表")
    @PostMapping("/queryPageList")
    public Result<Page<LogisticsBillOrderDepponSnapshotDTO>> queryPageList(LogisticsBillOrderDepponSnapshotBO bo){
        Wrapper<LogisticsBillOrderDepponSnapshotDTO> wrapper = new EntityWrapper<>();

        if(bo.getBillPlatId() != null) {
            wrapper.eq("bill_plat_id", bo.getBillPlatId());
        }
        if(bo.getBillId() != null){
            wrapper.eq("bill_id", bo.getBillId());
        }
        if(StrUtils.isNotEmpty(bo.getMailNo())){
            wrapper.like("mail_no", bo.getMailNo());
        }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());
        }
        wrapper.orderBy("status", false);
        wrapper.orderBy("order_time", false);
        Page<LogisticsBillOrderDepponSnapshotDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = logisticsBillOrderDepponSnapshotService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @PostMapping(value = "/batchExportLogisticOrderList")
    @ApiOperation(value = "pc批量导出物流订单表列表", notes = "pc批量导出物流订单表列表")
    public void batchExportOrderList(HttpServletResponse response, String ids,Long billId) {
        //创建一个数组用于设置表头
        String[] attributeNames = new String[]{"业务日期","运单号","用户名称","发货人","始发站","收货人","目的地","货物名称","货物重量","货物件数","总运费","是否支付"};
        //查询账单记录
        List<LogisticsBillOrderDepponSnapshotExpotVO> list = logisticsBillOrderDepponSnapshotService.queryExportList(ids,billId);
        BigDecimal money = new BigDecimal(0.00).setScale(2, BigDecimal.ROUND_UP);
        for (LogisticsBillOrderDepponSnapshotExpotVO logisticsBillOrderDepponSnapshotExpotVO : list) {
            if (!(logisticsBillOrderDepponSnapshotExpotVO.getPayStatus().equals("支付成功"))){
                if (logisticsBillOrderDepponSnapshotExpotVO.getDepponMoney()!=null){
                    money= money.add(logisticsBillOrderDepponSnapshotExpotVO.getDepponMoney());
                }
            }

        }
        ExcelExport2.export(response, list, attributeNames, "账单列表","工服MALL平台德邦月结客户对账单","应还金额："+money);
    }


}
